#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
using namespace std;


class Solution {
public:
    /**
     * @brief 文件路径简化
     * 1. // -> /
     * 2. ./当前路径
     * 3. ../上一级目录
     * 4. 以/开头，不以/结尾
     */
    string simplifyPath(string path) {
        vector<string> sp;
        int i = 0;
        while(i<path.size()){
            
            // cout<<path[i];

            if(path[i] == '.'){
                i++;
                if(i<path.size() && path[i] == '/'){
                    // ./ 不操作
                }
                else if(i<path.size() && path[i]=='.'){
                    // ..
                    i++;
                    if(i>=path.size() || (i<path.size() && path[i]=='/')){
                        // ../ 或者最后的 ..
                        if(!sp.empty()){
                            sp.pop_back();
                        }
                    }
                    else {
                        // ..X 此时是文件夹
                        string floder = "..";
                        while(i<path.size() && path[i]!='/'){
                            floder += path[i];
                            i++;
                        }
                        sp.push_back(floder);
                    }
                }else if(i<path.size() && path[i]!= '/'){
                    // .x
                    string floder = ".";
                    while(i<path.size() && path[i]!='/'){
                        floder += path[i];
                        i++;
                    }
                    sp.push_back(floder);
                }
            }else if(path[i]!= '/'){
                    string floder = "";
                    while(i<path.size() && path[i]!='/'){
                        floder += path[i];
                        i++;
                    }
                    sp.push_back(floder);
                }
            i++;
        }
        string ans = "/";
        for(int i=0;i<sp.size();i++){
            if(i == sp.size()-1){
                ans += sp[i];
            }else {
                ans += sp[i] + "/";
            }
        }
        return ans;
    }
};


int main(){
    Solution solution;
    //vector<vector<int>> input = {{1,3},{2,6},{8,10},{15,18}};
    // solution.maxSubArray(input);
    // solution.lengthOfLastWord("Hello World");
    string ans = solution.simplifyPath("/a/..//c/");
    cout<<ans<<endl;
    return 0;
}
